package com.ltu.timkor6.gamesite.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ltu.timkor6.gamesite.games.missingbits.client.MissingBits;
import com.ltu.timkor6.gamesite.games.suduku.client.Suduku;
import com.ltu.timkor6.gamesite.games.towerofhanoi.client.TowerOfHanoi;
import com.ltu.timkor6.gamesite.listings.client.Listings;
import com.ltu.timkor6.gamesite.shared.UserInfo;
import com.ltu.timkor6.gamesite.users.client.Users;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GameSite implements EntryPoint
{
	private Users users;
	
	private SimplePanel menuSignIn;
	private Anchor menuRegister;
	
	private Anchor menuUserList;
	private Anchor menuTopList;
	private SimplePanel menuSignOut;
	
	private HorizontalPanel body;
	private VerticalPanel menu;
	private SimplePanel content;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		this.users = new Users(this);
		
		// Check if user is logged in.
		this.users.checkIfLoggedIn();
	}
	
	public void loadModules()
	{
		UserInfo userInfo = this.users.getUserInfo();
		
		body = new HorizontalPanel();
		body.setStyleName("body");
		RootPanel.get("body").add(body);
		
		drawHeader();
		drawMenu();
		drawContent();
		
		if(userInfo == null)
		{
			this.addSignIn();
		}
		else
		{
			this.addSignOut();
		}
	}
	
	private void drawHeader()
	{
		
	}
	
	private void drawMenu()
	{
		menu = new VerticalPanel();
		menu.setStyleName("game-menu");
		
		final Label menuHeader = new Label("Menu");
		menuHeader.addStyleName("menuHeader");
		
		final Anchor menuMainPage = new Anchor("Main Page", "#mainpage");
		menuMainPage.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleMainPage();
			}
		});
		
		final Anchor menuGameMissingBits = new Anchor("Game: Missing Bits", "#game/missingbits");
		menuGameMissingBits.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleGameMissingBits();
			}
		});
		
		final Anchor menuGameSuduku = new Anchor("Game: Suduku", "#game/suduku");
		menuGameSuduku.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleGameSuduku();
			}
		});
		
		final Anchor menuGameTowerOfHanoi = new Anchor("Game: Tower Of Hanoi", "#game/towerofhanoi");
		menuGameTowerOfHanoi.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleGameTowerOfHanoi();
			}
		});
		
		menuUserList = new Anchor("UserList", "#userlist");
		menuUserList.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleListings(false);
			}
		});
		
		menuTopList = new Anchor("TopList", "#toplist");
		menuTopList.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleListings(true);
			}
		});
		
		menuSignIn = new SimplePanel();
		menuSignIn.addStyleName("menuSignIn");
		Anchor anchorSignIn = new Anchor("Sign In", "#signin");
		anchorSignIn.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleSignIn();
			}
		});
		this.menuSignIn.add(anchorSignIn);
		
		menuSignOut = new SimplePanel();
		menuSignOut.addStyleName("menuSignIn");
		Anchor anchorSignOut = new Anchor("Sign Out", "#signout");
		anchorSignOut.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleSignOut();
			}
		});
		this.menuSignOut.add(anchorSignOut);
		
		menuRegister = new Anchor("Register", "#register");
		menuRegister.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				moduleRegister();
			}
		});
		
		menu.add(menuHeader);
		menu.add(menuMainPage);
		menu.add(menuGameMissingBits);
		menu.add(menuGameSuduku);
		menu.add(menuGameTowerOfHanoi);
		
		this.body.add(this.menu);
	}
	
	private void drawContent()
	{
		this.content = new SimplePanel();
		this.content.setStyleName("game-content");
		
		this.moduleMainPage();
		
		this.body.add(this.content);
	}
	
	private void setContent(Widget child)
	{
		this.content.clear();
		this.content.add(child);
	}
	
	private void moduleMainPage()
	{
		this.setContent(new Label("Welcome"));
	}
	
	// Enable module greetings
	private void moduleGameMissingBits()
	{
		this.setContent(new MissingBits(users).getComposite());
	}
	
	// Enable module greetings
	private void moduleGameSuduku()
	{
		this.setContent(new Suduku(users).getComposite());
	}
	
	// Enable module greetings
	private void moduleGameTowerOfHanoi()
	{
		this.setContent(new TowerOfHanoi(users).getComposite());
	}
	
	// Enable module to sign in
	private void moduleListings(boolean all)
	{
		this.setContent(new Listings(all).getComposite());
	}
	
	// Enable module to sign in
	public void moduleSignIn()
	{
		this.setContent(users.getSignInComposite());
	}
	
	private void moduleSignOut()
	{
		this.users.sendSignOut();
	}
	
	private void moduleRegister()
	{
		this.setContent(users.getRegisterComposite());
	}
	
	public void changeSignedIn()
	{
		// Change the menu
		if(this.users.getUserInfo() != null)
		{
			this.delSignIn();
			this.addSignOut();
			this.moduleMainPage();
		}
		else
		{
			this.delSignOut();
			this.addSignIn();
		}
	}
	
	private void addSignIn()
	{
		this.menu.add(this.menuSignIn);
		this.menu.add(this.menuRegister);
	}
	
	private void delSignIn()
	{		
		this.menuSignIn.removeFromParent();
		this.menuRegister.removeFromParent();
	}
	
	private void addSignOut()
	{
		this.menu.add(this.menuUserList);
		this.menu.add(this.menuTopList);
		this.menu.add(this.menuSignOut);
	}
	
	private void delSignOut()
	{
		this.menuUserList.removeFromParent();
		this.menuTopList.removeFromParent();
		this.menuSignOut.removeFromParent();
	}
}
